using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace Pricom
{
  class Pricom
  {
    public void Connect(string proxy, string server, string channel, string port, string nick)
    {
      string url = nick + "@" + server + ":" + port;
      bool created = connections.ContainsKey(url);
      IConnectionProvider connection = (created ? GetConnection(url) : new ConnectionProviders.SmartIrc());
      if (!created || !GetConnection(url).IsConnected())
      {
        if (!created)
          connections.Add(url, connection);

        if (OnConnecting != null)
          OnConnecting.Invoke(proxy, server, channel, port, nick);

        connection.SetProxy(proxy);
        connection.Connect(server, Convert.ToInt32(port), nick);
      }

      if (connection.IsConnected())
      {
        if (channel.Length > 0)
        {
          if (OnConnected != null)
            OnConnected.Invoke(url + "/" + channel);
          connection.Join(channel);
        } 
        else if (OnConnected != null)
          OnConnected.Invoke(url);
      }
    }

    public void RemoveConnection(string url)
    {
      connections.Remove(url);
    }

    public void LoadConfig()
    {
      if ( System.IO.File.Exists("pricom.cfg") )
        Config.main = Config.Load("pricom.cfg");
      else
        Config.main = Config.Load("config.xml");

      foreach (Config.Connection conn in Config.main.connections)
      {
        Connect(conn.Proxy, conn.Server, conn.Channel, conn.Port, conn.Nick);
      }
      //if (saveTest)
      //{
      //  config.user.rsaParams = crypto.GenerateKeys("almakortebarack");
      //  config.user.encryptTest = crypto.Encrypt("proba");
      //  config.Save();
      //}
      //else
      //{
      //  config = Config.Load("config.xml");
      //  crypto.SetKeys(config.user.rsaParams, "almakortebarack");
      //  MessageBox.Show(crypto.Decrypt(config.user.encryptTest, "almakortebarack"));
      //}
    }

    public void Shutdown()
    {
      Config.main.SaveAs("pricom.cfg");

      foreach (KeyValuePair<string, IConnectionProvider> it in connections)
        it.Value.Disconnect();
    }

    public void CreateConnection(string url)
    {
      if (url.Contains("/"))
        url = url.Substring(0, url.LastIndexOf('/'));
      if (connections.ContainsKey(url))
        return;
      IConnectionProvider connection = new ConnectionProviders.SmartIrc();
      connections.Add(url, connection);
    }

    public IConnectionProvider GetConnection(string url)
    {
      if (url.Contains("/"))
        url = url.Substring(0, url.LastIndexOf('/'));
      if (!connections.ContainsKey(url))
        return null;
      return connections[url];
    }

    public delegate void ConnectingEventHandler(string proxy, string server, string channel, string port, string nick);
    public delegate void ConnectedEventHandler(string url);
    public event ConnectingEventHandler OnConnecting;
    public event ConnectedEventHandler OnConnected;
    private Dictionary<string, IConnectionProvider> connections = new Dictionary<string, IConnectionProvider>();
  }
}
